<?php


namespace app\api\controller;

use think\facade\Db;
class Base
{
    protected $APPID;

    protected $SECRET;

    public function __construct()
    {
        $miniApp = Db::name("system")->where("config","miniapp")->find();
        $this->APPID = $miniApp["value"];
        $this->SECRET = $miniApp["extend"];
    }

    public function getUid($sessionKey)
    {
        $UID = Db::name("user")->where("openid",cache($sessionKey))->value("uid");
        return $UID;
    }

    public  function getAccessToken()
    {
        $URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID ."&secret=" .$this->SECRET;
        $res = curlData($URL);
        return $res["access_token"];
    }

    public function checkText($content)
    {
        $Url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" .$this->getAccessToken();
        $res = curlData($Url,json_encode(["content" => $content],JSON_UNESCAPED_UNICODE),"POST");
        return $res["errcode"];
    }

    public function checkImage($Url)
    {
        $Url = " https://api.weixin.qq.com/wxa/img_sec_check?access_token=" .$this->getAccessToken();
        $res = curlData($Url,["media"  => new \CURLFile($Url)],"POST");
        dump($res);
        return $res["errcode"];
    }
}